import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get_navigation/get_navigation.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'package:get/get_navigation/src/routes/get_route.dart';

import 'RouteConfig/Route.dart';
import 'widgetInfo.dart';

void main() {
  runApp(const MyApp());
}

/**
 * 1.实现需求：首页使用gridview搭建组件：
 * 2.搜索功能
 * 3.按钮加简介
 * 4.懒加载功能
 * 5.组件类
 */
class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {


  String inputText = '';
  final TextEditingController controller = TextEditingController();
  List widgetList = Routes;
  @override
  void initState() {
    controller.addListener(() {
      print('JtTag:实时输入内容:${controller.text}');
      inputText = controller.text;
    });
    final stopwatch = Stopwatch()..start(); // 开始计时

    // 要测试的代码
    for (int i = 0; i < 1000000; i++) {
      final x = i * i;
    }

    stopwatch.stop(); // 停止计时

    print('执行耗时: ${stopwatch.elapsedMicroseconds} 微秒');
    print('执行耗时: ${stopwatch.elapsedMilliseconds} 毫秒');
    // TODO: implement initState
    super.initState();
  }

  List _filter({required String inputText}) {
    print('JtTag:_filter');
    return Routes.where((_) {
      return _.name.contains(inputText);
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    print('JtTag:testName:执行build');
    return GetMaterialApp(
        title: "GetX",
        home: Scaffold(
            appBar: AppBar(title:  TextButton(child: Text('测试组件'), onPressed: () {
              print('JtTag:testName:个数:${widgetList.length}:${widgetList}');
            })),
            body: Column(
              children: [
                //输入框
                Row(children: [
                  Expanded(
                      child: TextField(
                    decoration: const InputDecoration(),
                    controller: controller,
                  )),
                  TextButton(
                      onPressed: () {
                        print('JtTag:当前内容:${inputText}');
                        //执行筛选逻辑
                        setState(() {
                          widgetList = _filter(inputText: inputText);
                          print('JtTag:testName:个数:${widgetList.length}:${widgetList}');
                        });
                      },
                      child: const Text('搜索'))
                ]),
                //组件列表
                Expanded(
                    child: GridView.builder(
                  // =====================
                  // 👇 布局行为
                  // =====================

                  // 内容滚动方向：垂直（默认），可设置为 Axis.horizontal
                  scrollDirection: Axis.vertical,

                  // 是否反向滚动
                  reverse: false,

                  // 控制器（用于滚动监听）
                  controller: ScrollController(),

                  // 是否根据内容自适应尺寸（常与 shrinkWrap 联用）
                  primary: false,

                  // 如果为 true，则 GridView 的大小会根据内容自动调整
                  shrinkWrap: false,

                  // 边距
                  padding: const EdgeInsets.all(12.0),

                  // 滚动物理效果（弹性/滑动/无）
                  physics: const BouncingScrollPhysics(),

                  // =====================
                  // 👇 构建子项数量
                  // =====================

                  itemCount: widgetList.length, // 总共 20 个网格项

                  // =====================
                  // 👇 子项构建器
                  // =====================
                  itemBuilder: (context, index) {
                    return Container(
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: Colors.teal[100 * ((index % 8) + 1)],
                        borderRadius: BorderRadius.circular(12),
                      ),
                      child: TextButton(
                        onPressed: () {
                          Get.toNamed(widgetList[index].name);
                        },
                        child: Text(widgetList[index].name.split('/').last),
                      ),
                    );
                  },

                  // =====================
                  // 👇 网格布局规则
                  // =====================
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3, // 每行显示 3 个
                    mainAxisSpacing: 10, // 主轴方向间距
                    crossAxisSpacing: 10, // 横轴方向间距
                    childAspectRatio: 1.0, // 子项宽高比：宽 / 高
                  ),
                  // 也可以换成 SliverGridDelegateWithMaxCrossAxisExtent：
                  /*
        gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
          maxCrossAxisExtent: 150, // 每个格子的最大宽度
          mainAxisSpacing: 10,
          crossAxisSpacing: 10,
          childAspectRatio: 1.0,
        ),
        */
                )),
              ],
            )),
        initialRoute: "/",
        getPages: Routes);
  }
}
